package cn.icanci.loopstack.ddk.admin.biz.service;

import cn.icanci.loopstack.ddk.admin.biz.utils.EnvUtils;
import cn.icanci.loopstack.ddk.admin.biz.utils.IDHolder;
import cn.icanci.loopstack.ddk.common.enums.BooleanEnum;
import cn.icanci.loopstack.ddk.common.model.config.BaseVO;
import cn.icanci.loopstack.lsi.event.EventDispatcher;

import java.util.Date;

import javax.annotation.Resource;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/07 18:39
 */
public abstract class BaseService<T extends BaseVO> {
    /** label 格式化 */
    protected static final String DELETED_FORMAT     = "[无效] %s";

    /** label 格式化 */
    protected static final String NOT_DELETED_FORMAT = "[有效] %s";

    protected String getEnv() {
        return EnvUtils.getEnv();
    }

    private String genUuid() {
        return IDHolder.generateNoBySnowFlake("CFG");
    }

    @Resource
    protected EventDispatcher eventDispatcher;

    /**
     * 是否是插入
     *
     * @param t t
     * @return 返回是否是插入
     */
    protected boolean doInsert(T t) {
        return t.getId() == null || t.getId() == 0;
    }

    /**
     * 是否是删除的
     *
     * @param t t
     * @return 返回是否是删除的
     */
    protected boolean isDeleted(T t) {
        return t.getIsDelete() == 1;
    }

    /**
     * 插入之前的动作
     *
     * @param vo VO
     */
    protected void beforeInsert(T vo) {
        vo.setId(null);
        vo.setEnv(getEnv());
        vo.setIsDelete(BooleanEnum.NO.getCode());
        vo.setUuid(genUuid());
        vo.setCreateTime(new Date());
        vo.setUpdateTime(new Date());
    }

    /**
     * 更新之前的动作
     * 
     * @param vo VO
     */
    protected void beforeUpdate(T vo) {
        vo.setEnv(getEnv());
        vo.setUpdateTime(new Date());
    }

}
